package com.poem.controller;

 import com.poem.dto.AuthorsDTO;
 import com.poem.dto.AuthorsPageQueryDTO;
 import com.poem.dto.PoemsPageQueryDTO;
 import com.poem.entity.Poems;
 import com.poem.result.PageResult;
 import com.poem.result.Result;
 import com.poem.service.AuthorsService;
import com.poem.entity.Authors;
import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;

 import java.util.List;

/**
 * @ClassName: AuthorsController
 * @Author: YLY
 * @Description:
 * @Date: 2025/10/14
 * @Version: 1.0
 */
@RestController
@RequestMapping("/authors")
@Slf4j
public class AuthorsController {

    @Resource
    private AuthorsService authorsService;

    /**
     * 查询所有作者
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Authors> authors = authorsService.queryAllAuthors();
        return Result.success(authors);
    }

    /**
     * 修改作者
     * @param authorsDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody AuthorsDTO authorsDTO){
        log.info("修改作者:{}", authorsDTO);
        authorsService.updateAuthor(authorsDTO);
        return Result.success();
    }

    /**
     * 添加作者
     * @param authorsDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody AuthorsDTO authorsDTO){
        log.info("添加作者:{}", authorsDTO);
        authorsService.addAuthor(authorsDTO);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除作者:{}", ids);
        authorsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 分页查询
     * @param authorsPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<Authors>> pageQuery(AuthorsPageQueryDTO authorsPageQueryDTO){
        log.info("分页查询：{}", authorsPageQueryDTO);
        PageResult<Authors> pageResult = authorsService.pageQuery(authorsPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result<Authors> getById(@PathVariable Long id){
        log.info("查询作者：{}", id);
        Authors authors = authorsService.getById(id);
        return Result.success(authors);
    }

}
